package cn.danger.com.receiver;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.NotificationCompat;
import android.util.Log;

import java.util.Date;

import cn.bmob.v3.datatype.BmobDate;
import cn.danger.com.activity.MainActivity;
import cn.danger.com.activity.R;
import cn.danger.com.model.entity.Schedule;

/**
 * Created by 俊 on 2017/1/7.
 */
public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Schedule schedule = (Schedule) intent.getSerializableExtra("schedule");
        Log.i("danger", "receiver:" + schedule);
        Log.i("danger", "time:" + new BmobDate(new Date(Long.parseLong(schedule.getDate_start()))).getDate());

        Intent notificationIntent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, schedule.getId(), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentTitle("日程提醒");
        builder.setContentText(schedule.getContent());
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setTicker("日程提醒");
        if (schedule.getRemind_ring().equals("苹果line")) {
            builder.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.ring1));
        } else if (schedule.getRemind_ring().equals("口哨")) {
            builder.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.ring2));
        } else if (schedule.getRemind_ring().equals("闹钟")) {
            builder.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.ring3));
        } else if (schedule.getRemind_ring().equals("山谷回响")) {
            builder.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.ring4));
        } else if (schedule.getRemind_ring().equals("低音")) {
            builder.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.ring5));
        } else if (schedule.getRemind_ring().equals("清新")) {
            builder.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.ring6));
        }
        builder.setWhen(Long.parseLong(schedule.getDate_start()));
        builder.setContentIntent(pendingIntent);
        manager.notify(schedule.getId(), builder.build());
        context.sendBroadcast(new Intent("update_schedule_list"));
    }
}